$(function(){
    /**
     * 请求BI关键数据
     */
    $.ajax({
        url:BI_SERVER_NAME + "subject/queryPortalRevenu.action",
        type:"GET",
        data:{
            "userCode":userInfo.empCode,
            "deptDmId":userInfo.optDeptCode,
            "origin":"UAP",
            "moduleName":"login",
        },
        dataType:"jsonp",
        success:function(data){
            if( data.collaborativePortalRevenuList.length > 0 ){
                var biData = data.collaborativePortalRevenuList[0];

                if(biData){
                    biSwitch("当月总收入","active sumTurnover",biData.dailyReceipts,biData.targetValue,biData.monthTotal);
                    biSwitch("当月收入 · 零担","LTLTurnover",biData.dailyReceiptsLD,biData.targetValueLD,biData.monthTotalLD);
                    biSwitch("当月收入 · 快递","deliverTurnover",biData.dailyReceiptsKD,biData.targetValueKD,biData.monthTotalKD);
                    biSwitch("当月收入 · 整车","carloadTurnover",biData.dailyReceiptsZC,null,biData.monthTotalZC);
                    $(".turnover-module.chart.turnover").show();
                    //BI关键数据滚动
                    $('#myCarousel').carousel({interval:7000});
                }
            }
        }
    });
});

/**
 * 判断显示状态
 * @param yesterday
 * @param targetValue
 * @param completeValue
 */
function biSwitch(systemName,clazz,yesterday,targetValue,completeValue){

    var unit = 10000;

    //有目标值样式
    var s1Html = '<div class="item">' +
        '<div class="data-display-box">' +
        '<section class="turnover-header">' +
        '<h1>{systemName}</h1>' +
        '<span>单位：万元</span>' +
        '</section>' +
        '<div class="row turnover-Chart">' +
        '<div class="pieChart">' +
        '<div class="circle">' +
        '<div class="circle-inner">' +
        '<img src="resources/img/water.gif">' +
        '<div class="caption"></div>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<div class="row">' +
        '<div class="left-data-box"><div class="left-data">当月目标值<span></span></div></div>' +
        '<div class="tip-spile">|</div>' +
        '<div class="right-data-box"><div class="right-data">累计完成值<span></span></div></div>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<div class="day-income">' +
        '昨日收入<span></span>' +
        '</div>' +
        '</div>';

    //无目标值样式
    var s2Html = '<div class="item">' +
        '<div class="data-display-box">' +
        '<section class="turnover-header">' +
        '<h1>{systemName}</h1>' +
        '<span>单位：万元</span>' +
        '</section>' +
        '<div class="row turnover-Chart">' +
        '<div class="pieChart">' +
        '<div class="circle-car">' +
        '<img src="resources/img/circlecar.png">' +
        '<div class="caption-car"></div>' +
        '</div>' +
        '</div>' +
        '<div class="row">' +
        '<div class="turnover-data">累计完成值</div>' +
        '</div>' +
        '</div>' +
        '</div>' +
        '<div class="day-income">' +
        '昨日收入<span></span>' +
        '</div>' +
        '</div>';

    var $obj;

    if(targetValue && targetValue != 0){
        $obj = $(s1Html.replace("{systemName}",systemName));
        $obj.addClass(clazz);
        hasTargetValue($obj,yesterday/unit,targetValue/unit,completeValue/unit);
    }else{
        $obj = $(s2Html.replace("{systemName}",systemName));
        $obj.addClass(clazz);
        notTargetVale($obj,yesterday/unit,completeValue/unit);
    }
}

//有目标值
function hasTargetValue(obj,yesterday,targetValue,completeValue) {
    var chartValue=(completeValue/targetValue)*100;
    var pieChartValue=Math.round(chartValue);
    obj.find('.left-data').find('span').html(targetValue.toFixed(2));
    obj.find('.right-data').find('span').html(completeValue.toFixed(2));
    obj.find(".caption").html(pieChartValue+"%");
    if(pieChartValue<100){
        obj.find('.circle-inner img').css("top",'100%')
        obj.find('.circle-inner img').animate({top:(95-pieChartValue)+'%'},7000);
        if(pieChartValue<35){
            obj.find(".caption").css("top",'65%');
        }else{
            obj.find(".caption").css("top",(100-pieChartValue)+'%');
        }
    }else{
        obj.find('.circle-inner img').css("top",'100%')
        obj.find('.circle-inner img').animate({top:'-12%'},7000);
        obj.find(".caption").css("top",'40%');
    }
    obj.find('.day-income span').html(yesterday.toFixed(2));

    $("#myCarousel").children(".carousel-inner").append(obj);
}

//无目标值
function notTargetVale(obj,yesterday,completeValue) {
    obj.find(".caption-car").html(completeValue.toFixed(2));
    obj.find('.day-income span').html(yesterday.toFixed(2));

    $("#myCarousel").children(".carousel-inner").append(obj);
}